package org.ehe.erp.controller.product;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.exception.BusinessException;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.erp.domain.product.bo.ErpMaterialBo;
import org.ehe.erp.domain.product.vo.ErpMaterialVo;
import org.ehe.erp.service.product.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材料类型管理
 * @author :zhangnn
 * @className :ErpMaterialController
 * @description: TODO
 * @date 2025-09-08 14:49:39
 */
@RestController
@RequestMapping("/sys/material")
@Slf4j
public class ErpMaterialController extends BaseController {
    @Autowired
    private MaterialService materialService;

    /**
     * 分页查询
     */
    @SaCheckPermission("erp:material:list")
    @GetMapping("/page")
    public TableDataInfo<ErpMaterialVo> getMaterialPage(ErpMaterialBo request, PageQuery pageQuery) {

        return materialService.getMaterialPage(request,pageQuery);
    }

    /**
     * 获取详情
     */
    @SaCheckPermission("erp:material:view")
    @GetMapping("/{id}")
    public R<ErpMaterialVo> getMaterialDetail(@PathVariable Long id) {
        return R.ok("获取详情成功",materialService.getMaterialDetail(id));
    }

    /**
     * 新增材料
     */
    @SaCheckPermission("erp:material:add")
    @PostMapping("/add")
    public R<Long> addMaterial(@Valid @RequestBody ErpMaterialBo request) {

        try {
            Long id = materialService.addMaterial(request);
            return R.ok("新增成功");
        } catch (BusinessException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新材料
     */
    @SaCheckPermission("erp:material:edit")
    @PostMapping("/update")
    public R<Boolean> updateMaterial(@Valid @RequestBody ErpMaterialBo request) {
        if (request.getId() == null) {
            return R.fail("ID不能为空");
        }
        try {
            boolean success = materialService.updateMaterial(request);
            return R.ok( "更新成功");
        } catch (BusinessException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除材料
     */
    @SaCheckPermission("erp:material:remove")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteMaterial(@PathVariable Long id) {

        try {
            boolean success = materialService.deleteMaterial(id);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取所有启用的材料（下拉选择用）
     */
    @GetMapping("/all-enabled")
    public R<List<ErpMaterialVo>> getAllEnabledMaterials() {
        List<ErpMaterialVo> materials = materialService.getAllEnabledMaterials(LoginHelper.getTenantId());
        return R.ok(materials);
    }

    /**
     * 获取下一个可用编码
     */
    @GetMapping("/next-code")
    public R<String> getNextCode() {
        String tenantId = LoginHelper.getTenantId();
        String nextCode = materialService.getNextCode(tenantId);
        return R.ok(nextCode);
    }

    /**
     * 检查编码是否可用
     */
    @GetMapping("/check-code")
    public R<Boolean> checkCode(@RequestParam String materialCode,
                                @RequestParam(required = false) Long excludeId) {
        boolean available = materialService.isCodeAvailable(materialCode, excludeId);
        if(available){
           return R.ok(materialCode+" 编码可用");
        }
        return R.fail(materialCode+" 编码已存在");
    }
}
